package com.zhou.project.modules.circle.circlePostsYes.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.components.base.service.BaseService;
import com.zhou.project.modules.circle.circleGroupPosts.entity.CircleGroupPosts;
import com.zhou.project.modules.circle.circlePostsYes.dao.PostsYesDao;
import com.zhou.project.modules.circle.circlePostsYes.entity.PostYes;
import com.zhou.project.modules.system.user.entity.SysUser;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@CacheConfig(cacheNames = "postsYes")
public class PostsYesService extends BaseService<PostsYesDao, PostYes> {

    //查询圈子总点赞数
    public long queryYes(int postsId) {
        return baseMapper.queryYse(postsId);
    }

    //查询用户是否为某个话题点过一次赞
    @CacheEvict(allEntries = true)
    public boolean oneYes(int postsId) {
        int userId = currentAuthentication.getCurrentUserId();

        int a = baseMapper.oneYes(postsId, userId);

        return a == 0 ? true : false;

    }

    //取消点赞
    @Transactional
    @CacheEvict(allEntries = true)
    public void noYes(int postsId) {
        int userId = currentAuthentication.getCurrentUserId();
        baseMapper.noYes(postsId, userId);
    }
    //物理删除点赞
    @Transactional
    public void deleteTrue(int postsId){
        int userId = currentAuthentication.getCurrentUserId();
        baseMapper.deleteTrue(postsId,userId);
    }

    //我的圈子被谁点了赞
    @Cacheable(key = "'Yesuser:'+'#currentAuthentication.getCurrentUserId()'+':'+'#entity.size'")
    public List<SysUser> queryUser( PostYes entity) {
        //这里的userid 为我创建圈子的id
        int userId = currentAuthentication.getCurrentUserId();

        return baseMapper.queryUser(userId, entity.getSize());
    }

    //我点赞的圈子
    @Cacheable(key = "'Yespost:'+'#currentAuthentication.getCurrentUserId()'+':'+'#entity.size'")
    public List<CircleGroupPosts> queryPosts( PostYes entity) {

        int userId = currentAuthentication.getCurrentUserId();
        System.out.println(entity.getSize());
        return baseMapper.queryPosts(userId, entity.getSize());
    }

}
